package com.woniuxy.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.woniuxy.entity.ChooseCourse;
import com.woniuxy.entity.CoachFreeday;
import com.woniuxy.entity.CoachInfo;
import com.woniuxy.entity.Course;
import com.woniuxy.entity.Focus;
import com.woniuxy.entity.GymCoach;
import com.woniuxy.entity.GymInfo;
import com.woniuxy.entity.Style;
import com.woniuxy.entity.User;
import com.woniuxy.service.CoachInfoService;
import com.woniuxy.service.GymInfoService;
import com.woniuxy.util.CommonUtil;
/**
 * 场馆以及和场馆相关交互的controller层
 * 
 * @author 陈一珩
 *
 */
@Controller
@RequestMapping("/gymInfo")
public class GymInfoController {

	@Autowired // 通过uid获取场馆信息
	private GymInfoService gymInfoService;

	// 通过useId查询场馆
	@RequestMapping("/selectAllGymInfoByUserId")
	@ResponseBody
	public GymInfo selectAllGymInfoByUserId(Integer id) {
		return gymInfoService.selectAllGymInfoByUserId(id);

	};
	//传入十进制空闲时间转化为数组
	@RequestMapping("/getFreeSumArray")
	@ResponseBody
	public int[] getFreeSumArray(int freeSum) {
		int[] freeTime = CommonUtil.getFreeTime(freeSum);
		return freeTime;
		
	};
	//场馆所选的所有课程
	@RequestMapping("/allCoursesByGymId")
	@ResponseBody
	public List<ChooseCourse>  chooseCoursesById() {
		User user=(User) SecurityUtils.getSubject().getPrincipal();
		
		return gymInfoService.chooseCoursesById(user.getId());
		
	};
	//获取我的场馆信息
	@RequestMapping("/selectMyGymInfo")
	@ResponseBody
	public GymInfo sselectMyGymInfo() {
		User user=(User) SecurityUtils.getSubject().getPrincipal();
		int id = (int)user.getId();
		return gymInfoService.selectAllGymInfoByUserId(id);

	};

	// 查找我关注的人
		@RequestMapping("/focusMe")
		@ResponseBody
		public List<Focus> focusMe(Integer userId) {
			User user=(User) SecurityUtils.getSubject().getPrincipal();
			long id = user.getId(); 
			List<Focus> focus = gymInfoService.focusMe((int)id);
			return focus;

		};
	// 场馆招聘请求
	@RequestMapping("/myEmployee")
	@ResponseBody
	public Integer myEmployee(User user) {
		return 1;
	}
	@Autowired
	private CoachInfoService coachInfoService;

	// 展示所有场馆签约教练
	@RequestMapping("/mySginCoaches")
	@ResponseBody
	public List<CoachInfo> myEmployeeCoaches() {
		User user=(User) SecurityUtils.getSubject().getPrincipal();
		long id = user.getId(); 
		List<CoachInfo> selectAllGymCoachByUserId = gymInfoService.selectAllGymCoachByUserId((int)id);
		
		return selectAllGymCoachByUserId;
	}
	//mySginCoachesByLimit4 展示四个教练
	@RequestMapping("/mySginCoachesByLimit4")
	@ResponseBody
	public List<CoachInfo> mySginCoachesByLimit4() {
		User user=(User) SecurityUtils.getSubject().getPrincipal();
		long id = user.getId(); 
		List<CoachInfo> selectMySginCoachesByLimit4 = gymInfoService.selectMySginCoachesByLimit4((int)id);
		
		return selectMySginCoachesByLimit4;
	}
	//mySginCoachesByLimit4 展示四个教练
	@RequestMapping("/sginCoachesByLimit4ById")
	@ResponseBody
	public List<CoachInfo> sginCoachesByLimit4ById(int id) {
		
		List<CoachInfo> selectMySginCoachesByLimit4 = gymInfoService.selectMySginCoachesByLimit4(id);
		
		return selectMySginCoachesByLimit4;
	}
	//向我提交申请的教练
	@RequestMapping("/myPreEmp")
	@ResponseBody
	public List<CoachInfo> myPreEmp() {
		User user=(User) SecurityUtils.getSubject().getPrincipal();
		long id = user.getId(); 
		List<CoachInfo> myPreEmp = gymInfoService.myPreEmp((int) id);
		System.out.println(myPreEmp);
		return myPreEmp;
	}
	//签约教练
	@RequestMapping("/SginCoach")
	@ResponseBody
	public Integer SginCoach(Integer coachId) {
		User user=(User) SecurityUtils.getSubject().getPrincipal();
		long id = user.getId(); 
		Integer updateGymCoachSign = gymInfoService.updateGymCoachSign((int)id, coachId);
		return updateGymCoachSign;
	}
	//拒绝教练
	@RequestMapping("/refuseCoach")
	@ResponseBody
	public Integer refuseCoach(Integer coachId) {
		User user=(User) SecurityUtils.getSubject().getPrincipal();
		long id = user.getId(); 
		Integer refuseCoach = gymInfoService.updateGymCoachNoSign((int)id, coachId);
		return refuseCoach;
	}
	//和教练解约
	@RequestMapping("/deleteSginCoach")
	@ResponseBody
	public Integer deleteSginCoach(Integer coachId) {
		User user=(User) SecurityUtils.getSubject().getPrincipal();
		long id = user.getId(); 
		System.out.println(coachId+"  "+id);
		Integer deleteSginCoach = gymInfoService.deleteSginCoach((int)id, coachId);
		return deleteSginCoach;
	}
	//修改场馆信息
		@RequestMapping("/updateGymInfo")
		@ResponseBody
		public Integer updateGymInfo(GymInfo gymInfo, User user) {
			System.out.println(gymInfo);
//			System.out.println(user);

//			Map<String, Object> map = new HashMap<>();
//			map.put("currentUser", currentUser);
//			map.put("gyminfo", gyminfo);
//			map.put("user",user);

			CommonUtil.updateCurrentUser(user);
			return gymInfoService.updateGymInfo(gymInfo, user);
		}
		//排课
		@RequestMapping("/arrangeCourse")
		@ResponseBody
		public Integer arrangeCourse(ChooseCourse chooseCourse) {
			User user=(User) SecurityUtils.getSubject().getPrincipal();
			long id = user.getId(); 
			chooseCourse.setUserIdGym(id);
			return gymInfoService.arrangeCourse(chooseCourse);
		}
		//该教练的所有空闲时间
		@RequestMapping("/freeDayByCoachId")
		@ResponseBody
		public List<CoachFreeday> freeDayByCoachId(Long id) {
			
			return gymInfoService.freeDayByCoachId(id);
		}
		//所有流派
		@RequestMapping("/allStyles")
		@ResponseBody
		public List<Style> allStyles() {
			
			return gymInfoService.allStyles();
		}
		//所有课程
		@RequestMapping("/allCourses")
		@ResponseBody
		public List<Course> allCourses() {
			
			return gymInfoService.allCourses();
		}
		//上传场馆照片
		@RequestMapping("/upload")
		@ResponseBody
		public void upload(MultipartFile gymImage,HttpServletRequest request) {
			//获取文件名
			String filename = gymImage.getOriginalFilename();
			System.out.println(filename);
			
			//要处理文件名，避免用户上传相同类型、名字的文件出现覆盖情况
			int index = filename.lastIndexOf(".");
			String type = filename.substring(index);
			filename = UUID.randomUUID().toString()+type;
			
//			System.out.println("filename:"+filename);
			gymInfoService.addGymImg(filename);
			//获取存放文件的路径
			String path = request.getServletContext().getRealPath("");//当前项目的根路径
			System.out.println(path);
			File root = new File(path);//当前项目的根路径
			//得到存放文件的目录
			File fileDir = new File(root, "gymImg");
			if (!fileDir.exists()) {
				fileDir.mkdirs();	//创建
			}
			
			//得到文件存储的路径  files/文件名
			filename = fileDir.getAbsolutePath()+File.separator+filename;
			System.out.println(filename);
			
			File destFile = new File(filename);
			try {
				gymImage.transferTo(destFile);//写入磁盘
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		
		}
		
}
